package com.black.platform.organization.service.bz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.platform.organization.mapper.OrganizationMapper;
import com.black.platform.organization.po.Organization;
import com.black.common.util.ComCodeUtil;
import com.black.common.constant.ShareConstant;
import com.black.platform.organization.service.bz.OrganizationService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 组织机构基础服务
 */
@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements OrganizationService {

    /**
     * 如果没有配置，则使用默认的数据权限方案
     */
    @Value("${share.auth.default-data-power:true}")
    boolean defaultDataPower;

    /**
     * 新增组织机构
     *
     * @param organization 组织机构
     */
    @Override
    public void addOrganization(Organization organization) {
        this.baseMapper.insert(organization);
    }

    /**
     * 移除组织机构
     *
     * @param organization 组织机构
     * @return 成功：1 失败：0
     */
    @Override
    public int removeOrganization(Organization organization) {
        return this.baseMapper.deleteById(organization.getComCode());
    }

    /**
     * 关闭组织机构
     *
     * @param organization 组织机构
     * @return 成功：1 失败：0
     */
    @Override
    public int closeOrganization(Organization organization) {
        organization.setValid(ShareConstant.Status.IN_VALID);
        return this.baseMapper.updateById(organization);
    }

    /**
     * 更新组织机构信息
     *
     * @param organization 组织机构
     * @return 成功：1 失败：0
     */
    @Override
    public int updateOrganization(Organization organization) {
        return this.baseMapper.updateById(organization);
    }

    /**
     * 根据comCode向下获取所包含的归属机构
     *
     * @param comCode          归属机构代码
     * @param isIncludeInvalid 是否包含无效的
     * @return List<Organization> 归属机构集
     */
    @Override
    public List<Organization> getOrganizationsWithChildren(String comCode, String comName, boolean isIncludeInvalid) {
        List<Organization> organizationList;
        QueryWrapper<Organization> query = new QueryWrapper<>();
        if (!isIncludeInvalid) {
            query.eq("valid", ShareConstant.Status.VALID);
        }
        if (!StringUtils.isEmpty(comCode)) {
            organizationList = getOrganizations(comCode, query, this.baseMapper.getOrganizationsByPowerComCode(comCode, isIncludeInvalid));
            if (!StringUtils.isEmpty(comName)) {
                boolean anyMatch = organizationList.stream().anyMatch(o -> comCode.equals(o.getComCode()) && comName.equals(o.getComName()));
                if (!anyMatch) {
                    organizationList = new ArrayList<>();
                }
            }
        } else if (!StringUtils.isEmpty(comName)) {
            query.eq("com_name", comName);
            organizationList = this.baseMapper.selectList(query);
            QueryWrapper<Organization> query2 = new QueryWrapper<>();
            String comCode1 = organizationList.get(0).getComCode();
            organizationList = getOrganizations(comCode1, query2, this.baseMapper.getOrganizationsByPowerComCode(comCode, isIncludeInvalid));
        } else {
            organizationList = this.baseMapper.selectList(query);
        }
        return organizationList;
    }

    private List<Organization> getOrganizations(String comCode, QueryWrapper<Organization> query, List<Organization> organizationsByPowerComCode) {
        List<Organization> organizationList;
        if (defaultDataPower) {
            String powerComCode = ComCodeUtil.getPowerComCode(comCode);
            if (comCode.equals(powerComCode)) {
                query.eq("com_code", comCode);
            } else if (!StringUtils.isEmpty(powerComCode)) {
                query.like("com_code", powerComCode + "%");
            }
            organizationList = this.baseMapper.selectList(query);
        } else {
            organizationList = organizationsByPowerComCode;
        }
        return organizationList;
    }

    /**
     * 根据comCode向下获取所包含的归属机构代码
     *
     * @param comCode 归属机构代码
     * @return 归属机构代码集 格式: comCode,comCode,comCode...
     */
    @Override
    public String getPowerComCode(String comCode) {
        return this.baseMapper.getPowerComCode(comCode);
    }

}
